package me.zhengjie.modules.system.utils.common;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.system.domain.req.BankCardReqDTO;
import me.zhengjie.modules.system.domain.req.BankCardSubBranchReqDTO;
import me.zhengjie.modules.system.domain.req.IdCardReqDTO;
import me.zhengjie.modules.system.domain.rsp.BandCardRspDTO;
import me.zhengjie.modules.system.domain.rsp.BankCardSubBranchInfoRspDTO;
import me.zhengjie.modules.system.domain.rsp.BankCardSubBranchRspDTO;
import me.zhengjie.modules.system.domain.rsp.IdCardRspDTO;

import java.util.HashMap;
import java.util.List;

@Slf4j
public class ReadCardUtil {


    static String PATH = "http://t1.hbtuding.com/api/v1.yunfubao.other/ocr";
    static String BANK_PATH = "http://t1.hbtuding.com/api/v1.yunfubao.other/yinghang";


    public static IdCardRspDTO readIdCard(IdCardReqDTO idCardReqDTO) {
        IdCardRspDTO idCardRspDTO = IdCardRspDTO.builder().build();

        if (StrUtil.isNotBlank(idCardReqDTO.getFront())) {
            HashMap<Object, Object> hashMap = new HashMap<>();
            hashMap.put("ocrType", 0);
            hashMap.put("imgURL", idCardReqDTO.getFront());
            String post = HttpUtil.post(PATH, JSONUtil.toJsonStr(hashMap));
            HashMap resultMap = JSONUtil.toBean(post, HashMap.class);
            HashMap InfoMap = JSONUtil.toBean(Convert.toStr(resultMap.get("data")), HashMap.class);
            if (ObjectUtil.equal(InfoMap.get("code"), "0000") && ObjectUtil.equal(InfoMap.get("msg"), "识别成功")) {
                HashMap data = JSONUtil.toBean(Convert.toStr(InfoMap.get("data")), HashMap.class);
                HashMap frontInfo = JSONUtil.toBean(Convert.toStr(Convert.toStr(data.get("front"))), HashMap.class);
                idCardRspDTO.setName(Convert.toStr(frontInfo.get("name")));
                idCardRspDTO.setNationality(Convert.toStr(frontInfo.get("nationality")));
                idCardRspDTO.setBirthDay(DateUtil.toLocalDateTime(DateUtil.parse(Convert.toStr(frontInfo.get("birthDay")), "yyyy-MM-dd")));
                idCardRspDTO.setIdNumber(Convert.toStr(frontInfo.get("idNumber")));
                idCardRspDTO.setAddress(Convert.toStr(frontInfo.get("address")));
                idCardRspDTO.setGender(Convert.toStr(frontInfo.get("gender")));
            }
        }
        if (StrUtil.isNotBlank(idCardReqDTO.getBack())) {
            HashMap<Object, Object> hashMap = new HashMap<>();
            hashMap.put("ocrType", 1);
            hashMap.put("imgURL", idCardReqDTO.getBack());
            String post = HttpUtil.post(PATH, JSONUtil.toJsonStr(hashMap));
            HashMap resultMap = JSONUtil.toBean(post, HashMap.class);
            HashMap InfoMap = JSONUtil.toBean(Convert.toStr(resultMap.get("data")), HashMap.class);
            if (ObjectUtil.equal(InfoMap.get("code"), "0000") && ObjectUtil.equal(InfoMap.get("msg"), "识别成功")) {
                HashMap data = JSONUtil.toBean(Convert.toStr(InfoMap.get("data")), HashMap.class);
                HashMap backInfo = JSONUtil.toBean(Convert.toStr(Convert.toStr(data.get("back"))), HashMap.class);
                idCardRspDTO.setIssuedBy(Convert.toStr(backInfo.get("issuedBy")));
                idCardRspDTO.setValidDateStart(DateUtil.toLocalDateTime(DateUtil.parse(Convert.toStr(backInfo.get("validDateStart")), "yyyy.MM.dd")));
                idCardRspDTO.setValidDateEnd(DateUtil.toLocalDateTime(DateUtil.parse(Convert.toStr(backInfo.get("validDateEnd")), "yyyy.MM.dd")));
            }
        }
        return idCardRspDTO;
    }

//    public static void main(String[] args) {
//        Date date = DateUtil.parse("2029-01-16", "yyyy.MM.dd");
//        // 将 java.util.Date 转换为 LocalDateTime
//        LocalDateTime localDateTime = DateUtil.toLocalDateTime(date);
//        System.out.println("parse=" + localDateTime);
//    }


    public static BandCardRspDTO readBankCard(BankCardReqDTO bankCardReqDTO) {

        BandCardRspDTO bandCardRspDTO = new BandCardRspDTO();
        HashMap<Object, Object> hashMap = new HashMap<>();
        hashMap.put("ocrType", 2);
        hashMap.put("imgURL", bankCardReqDTO.getBankCardPath());
        String post = HttpUtil.post(PATH, JSONUtil.toJsonStr(hashMap));
        HashMap resultMap = JSONUtil.toBean(post, HashMap.class);
        HashMap InfoMap = JSONUtil.toBean(Convert.toStr(resultMap.get("data")), HashMap.class);
        if (ObjectUtil.equal(InfoMap.get("code"), "0000") && ObjectUtil.equal(InfoMap.get("msg"), "识别成功")) {
            HashMap data = JSONUtil.toBean(Convert.toStr(InfoMap.get("data")), HashMap.class);
            //eg:中国工商银行
            String bank = Convert.toStr(Convert.toStr(data.get("bank")));
            //eg:123456789
            String number = Convert.toStr(Convert.toStr(data.get("number")));
            bandCardRspDTO.setBank(bank);
            bandCardRspDTO.setNumber(number);
        }
        return bandCardRspDTO;
    }

    public static BankCardSubBranchInfoRspDTO readBankCardSubBranch(BankCardSubBranchReqDTO reqDTO) {
        BankCardSubBranchInfoRspDTO bankCardSubBranchInfoRspDTO = BankCardSubBranchInfoRspDTO.builder().build();
        HashMap<Object, Object> hashMap = new HashMap<>();
        hashMap.put("cardno", reqDTO.getBankCardNumber());
        hashMap.put("city", reqDTO.getCity());
        hashMap.put("province", reqDTO.getProvince());
        hashMap.put("page", reqDTO.getPage());
        String post = HttpUtil.post(BANK_PATH, JSONUtil.toJsonStr(hashMap));
        log.info("post=={}", post);
        HashMap resultMap = JSONUtil.toBean(post, HashMap.class);
        if (ObjectUtil.equal(Convert.toStr(resultMap.get("code")), "666")
                && ObjectUtil.equal(Convert.toStr(resultMap.get("msg")), "成功")) {
            HashMap InfoMap = JSONUtil.toBean(Convert.toStr(resultMap.get("data")), HashMap.class);
            Integer totalpage = Convert.toInt(InfoMap.get("totalpage"));
            List<BankCardSubBranchRspDTO> list = JSONUtil.toList(Convert.toStr(InfoMap.get("data")), BankCardSubBranchRspDTO.class);
            bankCardSubBranchInfoRspDTO.setBankCardSubBranchRspDTO(list);
            bankCardSubBranchInfoRspDTO.setTotalPage(totalpage);
        }else {
            throw new BadRequestException(Convert.toStr(resultMap.get("msg"),"查询失败"));
        }
        return bankCardSubBranchInfoRspDTO;
    }


}
